package com.burns.rbac.controller;

import com.burns.rbac.commons.exception.BusinessException;
import com.burns.rbac.commons.R;
import com.burns.rbac.commons.constants.code.UserCodeConstants;
import com.burns.rbac.commons.utils.JWTUtils;
import com.burns.rbac.context.UserContextHolder;
import com.burns.rbac.dto.UserDto;
import com.burns.rbac.entity.UserToken;
import com.burns.rbac.entity.User;
import com.burns.rbac.service.IUserService;
import com.burns.rbac.commons.utils.ValidationUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
public class LoginController {

    @Autowired
    private IUserService userService;

    @GetMapping("/testCookie")
    public void test(HttpServletResponse response) {
        Cookie cookie = new Cookie("key1", "value1");
        cookie.setPath("/");
        cookie.setMaxAge(60*60*24);
        response.addCookie(cookie);
    }

    /**
     * 返回当前登录的用户;
     *
     * @return
     */
    @GetMapping("/user")
    public R user() {
        UserToken userToken = UserContextHolder.get();
        return R.ok().put("userToken", userToken);
    }

    @PostMapping("/login1")
    @Deprecated
    public R login1(@RequestBody UserDto userDto) {
        if ("admin".equals(userDto.getUsername())) {
            return R.ok();
        } else {
            throw new BusinessException(UserCodeConstants.UserNotExist, "【" + userDto.getUsername() + "】用户不存在");
        }
    }

    @PostMapping("/login2")
    @Deprecated
    public R login2(@RequestBody UserDto userDto) {
        //1.非空判断
        String validMsg = ValidationUtil.validateOne(userDto);
        if (StringUtils.isNotBlank(validMsg)) {
            return R.error(validMsg);
        }

        //2、根据用户名查询用户的信息
        User user = userService.getUserByUsername(userDto);

        //3、判断用户是否存在
        if (user == null) {
            return R.error("用户名不存在");
        }

        //4、密码的对比;暂时不做加密处理
        if (StringUtils.equals(userDto.getPassword(), user.getPassword())) {
            return R.error("用户名或密码错误");
        }

        //5. 准备一个userToken的对象 生成token(前后端分离的项目都是基于token来做认证的)

        UserToken userToken = new UserToken();
        userToken.setUserId(user.getId());
        userToken.setUsername(user.getUsername());

        //6.基于UserToken对象创建一个Token字符串
        String token = JWTUtils.createToken(userToken);

        //7。把token返回给客户端
        return R.ok().put("token", token);
    }

    @PostMapping("/login")
    public R login(String username, String password) {
        //1.非空判断
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return R.error("用户名或密码为空");
        }

        //2、根据用户名查询用户的信息
        User user = userService.getUserByUsername(username);

        //3、判断用户是否存在
        if (user == null) {
            return R.error("用户名不存在");
        }

        //4、密码的对比;暂时不做加密处理
        if (!StringUtils.equals(password, user.getPassword())) {
            return R.error("用户名或密码错误");
        }

        //5. 准备一个userToken的对象 生成token(前后端分离的项目都是基于token来做认证的)

        UserToken userToken = new UserToken();
        userToken.setUserId(user.getId());
        userToken.setUsername(user.getUsername());

        //6.基于UserToken对象创建一个Token字符串
        String token = JWTUtils.createToken(userToken);

        //7。把token返回给客户端
        return R.ok().put("token", token);
    }
}
